package com.xl333.mysql.dao;

import com.xl333.mysql.annotation.FieldNotReflect;
import com.xl333.util.PropertiesUtil;
import com.xl333.util.ReflectUtil;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 七月的狮子
 * @date 2020/5/13
 */
public class DaoManager<T> {

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(PropertiesUtil.getValue("database.url"),
                PropertiesUtil.getValue("database.user"),
                PropertiesUtil.getValue("database.password"));
    }

    private void closeResultSet(ResultSet rs){
        try {
            if (rs != null) {
                rs.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void closeStatement(Statement stmt){
        try {
            if (stmt != null) {
                stmt.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void closeConnection(Connection conn){
        try {
            if (conn != null) {
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public List<T> query(String sql, Class cls) {
        List<T> list = new ArrayList<>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            stmt.execute(sql);
            rs = stmt.getResultSet();
            while (rs.next()) {
                T t = ReflectUtil.newInstance(cls);
                Field [] fields = cls.getDeclaredFields();
                for(Field field:fields){
                    if(field.getAnnotation(FieldNotReflect.class) != null){
                        continue;   //不需要反射注入值
                    }

                    field.setAccessible(true);
                    if(field.getType() == String.class) {
                        field.set(t, rs.getString(field.getName()));
                    }else if(field.getType() == Integer.class){
                        field.set(t, rs.getInt(field.getName()));
                    }
                }
                list.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResultSet(rs);
            closeStatement(stmt);
            closeConnection(conn);
        }
        return list;
    }

    public void query(String sql, Object[] param) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            for(int i = 0;i < param.length;i++){
                pstmt.setObject(i, param[i]);
            }
            rs = pstmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResultSet(rs);
            closeStatement(pstmt);
            closeConnection(conn);
        }

    }

}
